﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ePhoneBook.Models;
using System.IO;
using ePhoneBook.Report;
using ePhoneBook.ReportingWebService;
using System.Net;

namespace ePhoneBook.Controllers
{
    public class TaylorCustomerController : Controller
    {
        private static List<TaylorCustomerModel> _TaylorList;
        public static List<TaylorCustomerModel> TaylorList
        {
            get
            {
                if (_TaylorList == null)
                {
                    _TaylorList = new List<TaylorCustomerModel>();
                    _TaylorList.Add(
                        new TaylorCustomerModel
                        {
                            CustomerCode = Guid.NewGuid().ToString().Replace("-", ""),
                            CustomerFullName = "David Nguyen",
                            MobileNumber = "0909318603"
                        });
                }
                return _TaylorList;
            }
            set
            {
                _TaylorList = value;
            }
        }
        //
        // GET: /Taylor/

        public ActionResult Index()
        {
            ViewData["CurrentTime"] = DateTime.Now.ToString();
            return View(TaylorList);
        }

        //
        // GET: /Taylor/Details/5

        public ActionResult Details(string customerCode)
        {
            return View(_TaylorList.Where(c => c.CustomerCode == customerCode).FirstOrDefault());
        }

        //
        // GET: /Taylor/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Taylor/Create

        [HttpPost]
        public ActionResult Create(TaylorCustomerModel taylorCustomerModel)
        {
            try
            {
                // TODO: Add insert logic here
                if (!FindByCustomerCode(taylorCustomerModel))
                {
                    _TaylorList.Add(taylorCustomerModel);
                    return RedirectToAction("Index");
                }
                ModelState.AddModelError("CustomerCode", "Mã khách hàng bị trùng!");
                return View();
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Taylor/Edit/5

        public ActionResult Edit(string customerCode)
        {
            return View(_TaylorList.Where(c => c.CustomerCode == customerCode).FirstOrDefault());
        }

        //
        // POST: /Taylor/Edit/5

        [HttpPost]
        public ActionResult Edit(string customerCode, TaylorCustomerModel taylorCustomerModel)
        {
            try
            {
                // TODO: Add update logic here

                List<TaylorCustomerModel> taylorCustomerModels = _TaylorList.Where(c => c.CustomerCode == customerCode).ToList();
                if (taylorCustomerModels != null && taylorCustomerModels.Count == 1)
                {
                    int index = _TaylorList.FindIndex(FindByCustomerCode);
                    _TaylorList[index] = taylorCustomerModel;
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        private static bool FindByCustomerCode(TaylorCustomerModel taylorCustomerModel)
        {
            List<TaylorCustomerModel> taylorCustomerModels = _TaylorList.Where(c => c.CustomerCode == taylorCustomerModel.CustomerCode).ToList();
            if (taylorCustomerModels != null && taylorCustomerModels.Count == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //
        // GET: /Taylor/Delete/5

        public ActionResult Delete(string customerCode)
        {
            _TaylorList.RemoveAll(c => c.CustomerCode == customerCode);
            return View();
        }

        //
        // POST: /Taylor/Delete/5

        [HttpPost]
        public ActionResult Delete(string customerCode, TaylorCustomerModel taylorCustomerModel)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
